package com.beauty.dressing.module.mine.persenter;

import com.beauty.dressing.common.base.BasePresenter;
import com.beauty.dressing.common.bean.AgreementBean;
import com.beauty.dressing.common.bean.BaseObserver;
import com.beauty.dressing.common.bean.BaseParse;
import com.beauty.dressing.common.bean.DataBean;
import com.beauty.dressing.common.bean.LeaseBean;
import com.beauty.dressing.common.bean.LogisticsCompanyBean;
import com.beauty.dressing.common.bean.RefuseJudgeBean;
import com.beauty.dressing.common.bean.RefuseJudgeExtraParser;
import com.beauty.dressing.common.bean.ReturnDetailInfoBean;
import com.beauty.dressing.module.mine.persenter.contract.MyLeaseContract;
import com.beauty.dressing.rxandroid.api.ApiManager;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

public class MyLeasePresenter extends BasePresenter implements MyLeaseContract.Presenter {
    private static final String TAG = "MyLeasePresenter";
    private MyLeaseContract.ViewControl mViewControl;

    public MyLeasePresenter(MyLeaseContract.ViewControl mViewControl) {
        this.mViewControl = mViewControl;
    }

    @Override
    public void myLease(String member_id, String type, String token, String page) {
        Map<String, String> map = new HashMap<>();
        map.put("member_id", member_id);
        map.put("type", type);
        map.put("token", token);
        map.put("page", page);
        ApiManager.getInstence().getLeaseService().myLease(map)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<Object, LeaseBean>() {
                    @Override
                    protected void onSuccees(String message, Object data, List<LeaseBean> list, BaseParse extra) {
                        mViewControl.onMyLease(true, 200, message, list);
                    }

                    @Override
                    protected void onFailed(int code, String message, Object data, List<LeaseBean> list, BaseParse extra) {
                        mViewControl.onMyLease(false, code, data.toString(), list);
                    }
                });
    }

    @Override
    public void explain() {
        ApiManager.getInstence().getLeaseService().explain()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<DataBean, Object>() {
                    @Override
                    protected void onSuccees(String message, DataBean data, List<Object> list, BaseParse extra) {
                        mViewControl.onExplain(true, 200, message, data);
                    }

                    @Override
                    protected void onFailed(int code, String message, DataBean data, List<Object> list, BaseParse extra) {
                        mViewControl.onExplain(false, code, message, data);
                    }


                });
    }

    @Override
    public void agreement(String type) {
        Map<String, String> map = new HashMap<>();
        map.put("type", type);
        ApiManager.getInstence().getLeaseService().agreement(map)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<AgreementBean, Object>() {

                    @Override
                    protected void onSuccees(String message, AgreementBean data, List<Object> list, BaseParse extra) {
                        mViewControl.onAgreement(true, 200, message, data);
                    }

                    @Override
                    protected void onFailed(int code, String message, AgreementBean data, List<Object> list, BaseParse extra) {
                        mViewControl.onAgreement(false, code, message, data);
                    }

                });
    }

    @Override
    public void lostApply(long member_id, int order_id, String token) {
        Map<String, String> map = new HashMap<>();
        map.put("member_id", String.valueOf(member_id));
        map.put("order_id", String.valueOf(order_id));
        map.put("token", token);
        ApiManager.getInstence().getLeaseService().lostApply(map)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<Object, Object>() {

                    @Override
                    protected void onSuccees(String message, Object data, List<Object> list, BaseParse extra) {
                        mViewControl.onLostApply(true, 200, message);
                    }

                    @Override
                    protected void onFailed(int code, String message, Object data, List<Object> list, BaseParse extra) {
                        mViewControl.onLostApply(false, code, message);
                    }

                });
    }

    @Override
    public void refuseJudge(String token, long Member_id, int order_id) {
        Map<String, String> map = new HashMap<>();
        map.put("token", token);
        map.put("member_id", String.valueOf(Member_id));
        map.put("order_id", String.valueOf(order_id));
        ApiManager.getInstence().getLeaseService().refuseJudge(map)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<RefuseJudgeBean, Object>() {
                    @Override
                    protected void onSuccees(String message, RefuseJudgeBean data, List<Object> list, BaseParse extra) {
                        if (extra instanceof RefuseJudgeExtraParser) {
                            mViewControl.onRefuseJudge(true, 200, message, ((RefuseJudgeExtraParser) extra).type, data);

                        }
                    }

                    @Override
                    protected void onFailed(int code, String message, RefuseJudgeBean data, List<Object> list, BaseParse extra) {
                        if (extra instanceof RefuseJudgeExtraParser) {
                            mViewControl.onRefuseJudge(false, code, message, ((RefuseJudgeExtraParser) extra).type, data);
                        }
                    }
                });
    }

    @Override
    public void getLogisticsCompany() {
        ApiManager.getInstence().getLeaseService().getLogisticsCompany()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<Object, LogisticsCompanyBean>() {

                    @Override
                    protected void onSuccees(String message, Object data, List<LogisticsCompanyBean> list, BaseParse extra) {
                        mViewControl.onGetLogisticsCompany(true, 200, message, list);
                    }

                    @Override
                    protected void onFailed(int code, String message, Object data, List<LogisticsCompanyBean> list, BaseParse extra) {
                        mViewControl.onGetLogisticsCompany(false, code, message, list);
                    }
                });
    }

    @Override
    public void returnApply(String token, int Member_id, int order_id, String company, String number, double return_money) {
        Map<String, String> map = new HashMap<>();
        map.put("token", token);
        map.put("member_id", String.valueOf(Member_id));
        map.put("order_id", String.valueOf(order_id));
        map.put("company", company);
        map.put("number", number);
        map.put("return_money", String.valueOf(return_money));
        ApiManager.getInstence().getLeaseService().returnApply(map)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<Object, Object>() {

                    @Override
                    protected void onSuccees(String message, Object data, List<Object> list, BaseParse extra) {
                        mViewControl.onReturnApply(true, 200, message);
                    }

                    @Override
                    protected void onFailed(int code, String message, Object data, List<Object> list, BaseParse extra) {
                        mViewControl.onReturnApply(false, code, message);
                    }
                });
    }

    @Override
    public void returnDetail(String token, long member_id, int order_id) {
        Map<String, String> map = new HashMap<>();
        map.put("token", token);
        map.put("member_id", String.valueOf(member_id));
        map.put("order_id", String.valueOf(order_id));
        ApiManager.getInstence().getLeaseService().returnDetail(map)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<ReturnDetailInfoBean, Object>() {
                    @Override
                    protected void onSuccees(String message, ReturnDetailInfoBean data, List<Object> list, BaseParse extra) {
                        mViewControl.onReturnDetail(true, 200, message, data);
                    }

                    @Override
                    protected void onFailed(int code, String message, ReturnDetailInfoBean data, List<Object> list, BaseParse extra) {
                        mViewControl.onReturnDetail(true, 200, message, data);
                    }
                });
    }

    @Override
    public void getLogisticsInfo(String token, long member_id, String number) {
        Map<String, String> map = new HashMap<>();
        map.put("token", token);
        map.put("member_id", String.valueOf(member_id));
        map.put("number", String.valueOf(number));
        ApiManager.getInstence().getLeaseService().getLogisticsInfo(map)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<Object, Object>() {
                    @Override
                    protected void onSuccees(String message, Object data, List<Object> list, BaseParse extra) {
                        mViewControl.onGetLogisticsInfo(true, 200, message, data.toString());
                    }

                    @Override
                    protected void onFailed(int code, String message, Object data, List<Object> list, BaseParse extra) {
                        mViewControl.onGetLogisticsInfo(true, 200, message, data.toString());
                    }
                });
    }
}
